package net.sudot.chess.business.controller.admin.system;

import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.mp.api.WxMpMenuService;
import net.sudot.chess.business.wechat.api.WechatApiService;
import net.sudot.chess.business.wechat.config.WechatConsts;
import net.sudot.chess.config.SystemProperties;
import net.sudot.chess.constant.LockConstants;
import net.sudot.chess.exception.SystemException;
import net.sudot.commons.json.JsonUtils;
import net.sudot.commons.lock.Lock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 微信菜单
 *
 * @author tangjialin on 2018-03-04
 */
@RestController
@RequestMapping("/admin/wechat/menus")
public class WechatMenuController {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private SystemProperties systemProperties;
    @Resource
    private WechatApiService wechatApiService;

    @PostMapping
    @ResponseStatus(code = HttpStatus.NO_CONTENT)
    @Lock(name = LockConstants.WECHAT_MENU_UPDATE)
    public void update() {
        String siteUrl = systemProperties.getWebsite();
        WxMpMenuService menuService = wechatApiService.getMenuService();
        // 左侧菜单
        WxMenuButton leftButton = new WxMenuButton();
        leftButton.setUrl(wechatApiService.oauth2buildAuthorizationUrl(String.format("%s/wechat/route?redirect=/", siteUrl), "snsapi_base", ""));
        leftButton.setType("view");
        leftButton.setName("比赛大厅");

        WxMenuButton centerButton = new WxMenuButton();
        centerButton.setName("个人中心");
        centerButton.setUrl(wechatApiService.oauth2buildAuthorizationUrl(String.format("%s/wechat/route?redirect=/customer/index", siteUrl), "snsapi_base", ""));
        centerButton.setType("view");

        // 右侧菜单
        List<WxMenuButton> moreSubButtons = new ArrayList<>();
        WxMenuButton rightButton = new WxMenuButton();
        rightButton.setName("更多服务");
        rightButton.setSubButtons(moreSubButtons);

        // 下载游戏
        WxMenuButton downloadButton = new WxMenuButton();
        downloadButton.setType("view");
        downloadButton.setUrl("http://wb.ihuizhi.com/chess/invite/downInvite");
        downloadButton.setName("下载游戏");
        moreSubButtons.add(downloadButton);

        // 联系客服
        WxMenuButton customerServiceButton = new WxMenuButton();
        customerServiceButton.setType("click");
        customerServiceButton.setKey(WechatConsts.KefuMsgType.TRANSFER_CUSTOMER_SERVICE);
        customerServiceButton.setName("联系客服");
        moreSubButtons.add(customerServiceButton);

        List<WxMenuButton> menuButtonList = new ArrayList<>();
        menuButtonList.add(leftButton);
        menuButtonList.add(centerButton);
        menuButtonList.add(rightButton);
        WxMenu wxMenu = new WxMenu();
        wxMenu.setButtons(menuButtonList);
        if (logger.isDebugEnabled()) {
            logger.debug("菜单更新请求数据:{}", JsonUtils.toPrettyJson(wxMenu));
        }
        try {
            String menuCreate = menuService.menuCreate(wxMenu);
            logger.debug("菜单更新响应数据:{}", menuCreate);
        } catch (Exception e) {
            throw new SystemException("微信公众号菜单更新异常", e);
        }
    }
}
